package org.komarichin.jpa.dao;

import org.komarichin.jpa.entity.IRole;
import org.komarichin.jpa.entity.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Michael Komarichin
 * Date: 28.09.2009
 * Time: 13:12:55
 */
@Repository
@Transactional(readOnly = true)
public class RoleDAO extends JpaDaoSupport implements IRoleDAO {
    private static final Logger log = LoggerFactory.getLogger(RoleDAO.class);

    /**
     * {@inheritDoc}
     */
    public IRole getRoleById(Long id) {
        log.debug("getRoleById#id : {}", id);
        return getJpaTemplate().find(Role.class, id);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public IRole getRoleByName(String name) {
        log.debug("getRoleByName#name : {}", name);
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", name);
        List<IRole> roles = getJpaTemplate().findByNamedQueryAndNamedParams("role.findByName", params);
        return roles != null && roles.size() > 0 ? roles.get(0) : null;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public IRole createRole(String name, Long permission) {
        log.debug("createRole#name: {}, permission: {}", name, permission);
        IRole role = new Role();
        role.setName(name);
        role.setPermission(permission);
        save(role);
        return role;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<IRole> getAll() {
        log.debug("getAll");
        return getJpaTemplate().findByNamedQuery("role.findAll");
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<IRole> getAllowRoles(Long permission) {
        log.debug("getAllowRoles#permission : {}", permission);
        Map<String, Long> params = new HashMap<String, Long>();
        params.put("permission", permission);
        return getJpaTemplate().findByNamedQueryAndNamedParams("role.findAllowRolesByPermission", params);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void save(IRole object) {
        log.debug("save#object : {}", object);
        getJpaTemplate().persist(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void update(IRole object) {
        log.debug("update#object : {}", object);
        getJpaTemplate().merge(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void saveOrUpdate(IRole object) {
        log.debug("saveOrUpdate#object : {}", object);
        getJpaTemplate().merge(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void delete(IRole object) {
        log.debug("delete#object : {}", object);
        getJpaTemplate().merge(object);
    }

}
